﻿using System;
using System.Collections.Generic;

namespace Program18
{
     internal class Program
    {
        private static void Main(string[] args)
        {
            //用方法实现:计算1-100之间的质数的和
            //质数就是只能被1和它本身整除的整数2，3，5，7，11，13，17，19，23.。。。
            Console.WriteLine("请输入一个范围");
            int n = Convert.ToInt32(Console.ReadLine());
            var list = new List<int>();
            var sum = GetPrimeSum(n, out list);
            foreach (var item in list)
            {
                Console.Write("{0}", item);
            }
            Console.WriteLine("\r\n总和:{0}", sum);
        }
        /// <summary>
        /// 判断是否为质数
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public static bool IsPrime(int n)
        {
            if (n < 2)
                return false;
            else
                for(var i = 2; i < n / 2; i++)
                {
                    if (n % i == 0)
                        return false;
                }
            return true;
        }

        /// <summary>
        /// 求N以内质数和
        /// </summary>
        /// <param name="N"></param>
        /// <returns></returns>
        public static int GetPrimeSum(int N,out List<int>list)
        {
            list = new List<int>();
            int sum = 0;
            for(var i = 2; i <= N; i++)
            {
                if (IsPrime(i))
                {
                    list.Add(i);
                    sum += i;
                }
            }
            return sum;
        }
    }
}
